﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;
using JCNT.DAL;
using Pub;
//using JCNT.NoteWeb.Pub;

namespace JCNT.NoteWeb.Web
{
    public partial class WP_Edt : System.Web.UI.Page
    {
        CADO ado1 = new CADO();
        C_CMS cms = new C_CMS();
        DataSet ds1;
        string s_sql = "";
        static string s_id;
        static string s_typ = "1";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] != null) s_id = Request["id"];
            if (Request["typ"] != null) s_typ = Request["typ"];

            if (!IsPostBack)
            {
                if (Session["login"] == null)
                {
                    //跳转到登入页面                
                    Response.Write("<script>alert('您的登录信息过期，请重新登录！');</script>");
                    //Response.Write("<script>window.opener = null;parent.close();window.open ('login.aspx');</script>");
                    Response.Redirect("Login.aspx");
                }


                Fill_ds();

                //修改数据
                if (s_id != null && s_typ == "0")
                    ShowInfo(Convert.ToInt32(s_id));

            }
        }

        public void Fill_ds()
        {

            tbox_Nlab.Visible = false;
            btn_lab_save.Visible = false;

            s_sql = "select * from wp_NLab";
            dlst_lab.DataSource = ado1.sel1(s_sql);
            dlst_lab.DataTextField = "Lab";
            dlst_lab.DataValueField = "ID";
            dlst_lab.DataBind();

            /*
            s_id = Request.QueryString["id"].ToString();
            s_typ = Request.QueryString["typ"].ToString();
            if (s_typ == "1") return;

            s_sql = "select id,tit,con,lab,oth,setDat from wp_note where id = " + s_id;
            ds1 = ado1.sel1(s_sql);
            tbox_tit.Text = ds1.Tables[0].Rows[0]["tit"].ToString();
            tbox_con.Text = ds1.Tables[0].Rows[0]["con"].ToString();
            tbox_oth.Text = ds1.Tables[0].Rows[0]["oth"].ToString();
            dlst_lab.Text = ds1.Tables[0].Rows[0]["lab"].ToString();
            */
            //lab_dat.Text = ds1.Tables[0].Rows[0]["setDat"].ToString();

        }

        private void ShowInfo(int ID)
        {
            JCNT.BLL.DB.wp_Note bll = new JCNT.BLL.DB.wp_Note();
            JCNT.Model.DB.wp_Note model = bll.GetModel(ID);
            //this.lblID.Text = model.ID.ToString();
            this.tbox_tit.Text = model.Tit;
            this.tbox_con.Text = model.Con;
            this.dlst_lab.Text = model.Lab;
            //this.txtSetDat.Text = model.SetDat.ToString();
            //this.txtEdiDat.Text = model.EdiDat.ToString();
            this.tbox_oth.Text = model.Oth;
            //this.txtPas1.Text = model.Pas1;
            //this.txtpas2.Text = model.pas2;
            //this.txtpas3.Text = model.pas3;
            //this.txtpas4.Text = model.pas4;
            //this.txtRootID.Text = model.RootID.ToString();
            //this.txtDat.Text = model.Dat.ToString();

        }

        protected void btn_ok_Click(object sender, EventArgs e)
        {


            s_id = Request.QueryString["id"].ToString();
            string s_tit = tbox_tit.Text;
            string s_con = tbox_con.Text;
            string s_oth = tbox_oth.Text;
            string s_lab = dlst_lab.SelectedItem.Text;
            string s_dat = DateTime.Now.ToString();

            if (tbox_tit.Text == "")
            {
                lab_info.Text = "标题不能为空！";
                return;
            }

            if (tbox_con.Text == "")
            {
                lab_info.Text = "内容不能为空！";
                return;
            }

            if (Session["Edit_Type"] == null)
            {
                Response.Redirect("Right.aspx");
            }
            //修改数据
            if (Session["Edit_Type"].ToString() == "0")
            {

                int ID = int.Parse(Session["nodeID"].ToString());
                JCNT.BLL.DB.wp_Note bll = new JCNT.BLL.DB.wp_Note();
                JCNT.Model.DB.wp_Note model = bll.GetModel(ID);
                string Tit = this.tbox_tit.Text;
                string Con = this.tbox_con.Text;
                string Lab = this.dlst_lab.Text;
                DateTime EdiDat = DateTime.Now;
                string Oth = this.tbox_oth.Text;
                int RootID = int.Parse(this.dlst_lab.SelectedValue);

                model.ID = ID;
                model.Tit = Tit;
                model.Con = Con;
                model.Lab = Lab;
                model.EdiDat = EdiDat;
                model.Oth = Oth;
                model.RootID = RootID;

                bll.Update(model);
                //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改成功！", "Right.aspx");
            }
            else if (Session["Edit_Type"].ToString() == "1")        //添加记录
            {
                string Tit = this.tbox_tit.Text;
                string Con = this.tbox_con.Text;
                string Lab = this.dlst_lab.Text;
                DateTime SetDat = DateTime.Now;
                DateTime EdiDat = DateTime.Now;
                string Oth = this.tbox_oth.Text;
                int RootID = int.Parse(this.dlst_lab.SelectedValue);

                JCNT.BLL.DB.wp_Note bll = new JCNT.BLL.DB.wp_Note();
                JCNT.Model.DB.wp_Note model = new JCNT.Model.DB.wp_Note();
                model.ID = bll.GetMaxId() + 1;
                model.Tit = Tit;
                model.Con = Con;
                model.Lab = Lab;
                model.SetDat = SetDat;
                model.EdiDat = EdiDat;
                model.Oth = Oth;
                model.RootID = RootID;

                bll.Add(model);
                //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功！", "Right.aspx");
                Session["nodeID"] = model.ID;
            }
            Response.Redirect("Default.aspx");

        }

        protected void btn_cancel_Click(object sender, EventArgs e)
        {
            lab_info.Visible = false;
            //s_id = Request.QueryString["id"].ToString();
            Response.Redirect("Right.aspx?id=" + s_id);
            //Response.Write("<script>javascript:window.history.back(-1); </script>");
            //Response.Redirect("Default.aspx");
        }

        protected void btn_lab_add_Click(object sender, EventArgs e)
        {
            tbox_Nlab.Visible = true;
            btn_lab_save.Visible = true;
        }

        protected void btn_lab_save_Click(object sender, EventArgs e)
        {
            dlst_lab.Items.Add(tbox_Nlab.Text);
            dlst_lab.Text = tbox_Nlab.Text;
            //for (int i = 0; i < dlst_lab.Items.Count; i++)
            //{
            //    if (dlst_lab.Items[i].Text == tbox_Nlab.Text)
            //        dlst_lab.SelectedIndex = i;
            //}
            tbox_Nlab.Visible = false;
            btn_lab_save.Visible = false;
        }
    }
}
